<template>
  <view class="container">
    <uni-card>
      <!-- 搜索区域 -->
      <view class="uni-search">
        <uni-search-bar
          v-model="queryParams.dictLabel"
          placeholder="请输入字典标签"
          @confirm="handleQuery"
        />
      </view>
      
      <!-- 操作按钮区域 -->
      <view class="uni-group">
        <button type="primary" @click="handleAdd" size="mini">新增</button>
      </view>

      <!-- 列表区域 -->
      <uni-list>
        <uni-list-item v-for="(item, index) in dataList" :key="index"
          :title="item.dictLabel"
          :note="item.dictValue"
          :rightText="item.status === '0' ? '正常' : '停用'"
          clickable
          @click="handleUpdate(item)"
        >
          <template v-slot:footer>
            <view class="uni-group">
              <button type="primary" size="mini" @click.stop="handleUpdate(item)">修改</button>
              <button type="warn" size="mini" @click.stop="handleDelete(item)" style="margin-left: 10px;">删除</button>
            </view>
          </template>
        </uni-list-item>
      </uni-list>
    </uni-card>

    <!-- 添加或修改字典数据弹窗 -->
    <uni-popup ref="popup" type="dialog">
      <uni-popup-dialog
        :title="title"
        mode="input"
        :value="form.dictLabel"
        placeholder="请输入字典标签"
        @confirm="submitForm"
      >
        <uni-forms ref="form" :model="form" :rules="rules">
          <uni-forms-item label="字典标签" required name="dictLabel">
            <uni-easyinput v-model="form.dictLabel" placeholder="请输入字典标签"/>
          </uni-forms-item>
          <uni-forms-item label="字典键值" required name="dictValue">
            <uni-easyinput v-model="form.dictValue" placeholder="请输入字典键值"/>
          </uni-forms-item>
          <uni-forms-item label="显示顺序" required name="dictSort">
            <uni-easyinput v-model="form.dictSort" type="number" placeholder="请输入显示顺序"/>
          </uni-forms-item>
          <uni-forms-item label="状态" name="status">
            <uni-data-checkbox
              v-model="form.status"
              :localdata="[
                {text: '正常', value: '0'},
                {text: '停用', value: '1'}
              ]"
            />
          </uni-forms-item>
          <uni-forms-item label="备注" name="remark">
            <uni-easyinput v-model="form.remark" type="textarea" placeholder="请输入备注"/>
          </uni-forms-item>
        </uni-forms>
      </uni-popup-dialog>
    </uni-popup>
  </view>
</template>

<script>
import { listData, getData, delData, addData, updateData } from '@/api/system/dict/data'

export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 字典数据列表
      dataList: [],
      // 弹出层标题
      title: "",
      // 字典类型
      dictType: undefined,
      // 查询参数
      queryParams: {
        dictType: undefined,
        dictLabel: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dictLabel: {
          rules: [{
            required: true,
            errorMessage: '字典标签不能为空'
          }]
        },
        dictValue: {
          rules: [{
            required: true,
            errorMessage: '字典键值不能为空'
          }]
        },
        dictSort: {
          rules: [{
            required: true,
            errorMessage: '显示顺序不能为空'
          }]
        }
      }
    };
  },
  onLoad(options) {
    this.dictType = options.dictType;
    this.getList();
  },
  methods: {
    /** 查询字典数据列表 */
    getList() {
      this.loading = true;
      this.queryParams.dictType = this.dictType;
      listData(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.title = "添加字典数据";
      this.$refs.popup.open();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getData(row.dictCode).then(response => {
        this.form = response.data;
        this.title = "修改字典数据";
        this.$refs.popup.open();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.form.validate().then(res => {
        this.form.dictType = this.dictType;
        if (this.form.dictCode != undefined) {
          updateData(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.$refs.popup.close();
            this.getList();
          });
        } else {
          addData(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.$refs.popup.close();
            this.getList();
          });
        }
      }).catch(error => {
        console.log('表单错误信息：', error);
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      uni.showModal({
        title: '提示',
        content: '是否确认删除标签为"' + row.dictLabel + '"的数据项?',
        success: res => {
          if (res.confirm) {
            delData(row.dictCode).then(() => {
              this.getList();
              this.$modal.msgSuccess("删除成功");
            })
          }
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        dictCode: undefined,
        dictLabel: undefined,
        dictValue: undefined,
        dictSort: 0,
        status: '0',
        remark: undefined
      };
    }
  }
};
</script>

<style>
.container {
  padding: 20rpx;
}
.uni-search {
  padding: 10rpx 0;
}
.uni-group {
  margin: 20rpx 0;
  display: flex;
  justify-content: flex-end;
}
</style> 